﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;

namespace TestProject
{    
    public partial class SetGenPhenLink : Form
    {
        private static GenomePhenotypeLink link;
        private bool value = false;
        private static decimal max_degree;
        private static int id;
        private static GeneTable gen_t;
        private string name;
        private int difference;
        public SetGenPhenLink(GenomePhenotypeLink l, decimal d, int i, string n)
        {
            InitializeComponent();
            link = l;
            max_degree = d;
            id = i;
            gen_t = new GeneTable(link.path_to_gene_table);
            name = n;
        }
        public SetGenPhenLink(GenomePhenotypeLink l, int i,decimal d, int diff)
        {
            InitializeComponent();
            link = l;
            id = i;
            difference = diff;
            value = true;
            max_degree = d;
            gen_t = new GeneTable(link.path_to_gene_table);
        }
        private void gene_changed(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            TableLayoutPanelCellPosition pos = tableLayoutPanel1.GetCellPosition(cb);
            tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(2, pos.Row));
            GeneType g=null;
            foreach (GeneType gg in gen_t.gene_table)
            {
                if ((string)cb.SelectedItem==gg.name)
                {
                    g=gg;
                    break;
                }
            }
            TableLayoutPanel t = new TableLayoutPanel();
            t.AutoSize = true;
            t.ColumnCount = 2;
            t.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            t.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            t.RowCount = g.amount_alelles;
            for (int i = 0; i < g.amount_alelles; i++)
            {
                Label l = new Label();
                l.Anchor = AnchorStyles.Left;
                l.AutoSize = true;
                l.Text = "Amount allele #" + i.ToString();
                NumericUpDown amount_box = new NumericUpDown();                
                amount_box.Size = new System.Drawing.Size(50, 23);                
                t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                t.Controls.Add(l, 0, i);
                t.Controls.Add(amount_box, 1, i);
            }
            tableLayoutPanel1.Controls.Add(t, 2, pos.Row);
        }
        private void SetGenPhenLink_Load(object sender, EventArgs e)
        {            
            name_box.Text = link.name;       
            if (!value)
            {
                for (int i = 1; i <= max_degree; i++)
                {
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    Label l = new Label();
                    ComboBox cb = new ComboBox();
                    cb.SelectedIndexChanged += new EventHandler(gene_changed);
                    cb.Anchor = AnchorStyles.Left;
                    l.Anchor = AnchorStyles.Left;
                    l.AutoSize = true;
                    l.Text = "Value: " + i.ToString();
                    foreach (GeneType g in gen_t.gene_table)
                    {
                        cb.Items.Add(g.name);
                    }
                    tableLayoutPanel1.Controls.Add(l, 0, i);
                    tableLayoutPanel1.Controls.Add(cb, 1, i);
                    tableLayoutPanel1.RowCount++;
                }
            }
            else
            {
                for (int i = 1; i <= difference; i++)
                {
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    Label l = new Label();
                    l.Text = "Value: " + (max_degree + i).ToString();
                    l.Anchor = AnchorStyles.Left;
                    l.AutoSize = true;
                    ComboBox cb = new ComboBox();
                    cb.SelectedIndexChanged += new EventHandler(gene_changed);
                    cb.Anchor = AnchorStyles.Left;
                    foreach (GeneType g in gen_t.gene_table)
                    {
                        cb.Items.Add(g.name);
                    }
                    tableLayoutPanel1.Controls.Add(l, 0, i );
                    tableLayoutPanel1.Controls.Add(cb, 1, i);
                    tableLayoutPanel1.RowCount++;
                }
            }             
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(link.path));
            if (!value)
            {
                XmlElement el = doc.CreateElement("characteristic");
                el.SetAttribute("Id", id.ToString());
                el.SetAttribute("Name", name);
                XmlNode values_node = doc.CreateElement("values");
                for (int i = 1; i <= max_degree; i++)
                {
                    XmlElement value_node = doc.CreateElement("value");
                    value_node.SetAttribute("Number", i.ToString());
                    XmlNode genes_node = doc.CreateElement("Genes");
                    XmlElement gene = doc.CreateElement("Gene");
                    GeneType g = null;
                    ComboBox cb = (ComboBox)tableLayoutPanel1.Controls[2 + 2 * i];
                    foreach (GeneType gg in gen_t.gene_table)
                    {
                        if (gg.name == (string)cb.SelectedItem)
                        {
                            g = gg;
                            break;
                        }
                    }
                    if (g == null)
                    {
                        MessageBox.Show("Value #" + i.ToString() + " has no link. Please, set it.");
                        return;
                    }
                    gene.SetAttribute("Id", g.allele_type_id.ToString());
                    gene.SetAttribute("Name", g.name);
                    TableLayoutPanel t = (TableLayoutPanel)tableLayoutPanel1.GetControlFromPosition(2, i + 1);
                    gene.SetAttribute("Amount", "0");
                    decimal amount = 0;
                    for (int j = 0; j < t.RowCount; j++)
                    {
                        NumericUpDown num = (NumericUpDown)t.Controls[1 + 2 * j];
                        gene.SetAttribute("AlleleAmount" + j.ToString(), num.Value.ToString());
                        amount += num.Value;
                    }
                    gene.Attributes[2].Value = amount.ToString();
                    genes_node.AppendChild(gene);
                    value_node.AppendChild(genes_node);
                    values_node.AppendChild(value_node);
                }
                el.AppendChild(values_node);
                doc.GetElementsByTagName("characteristics")[0].AppendChild(el);
                doc.Increment(doc.DocumentElement.Attributes[4]);
            }
            else
            {
                foreach (XmlNode node in doc.GetElementsByTagName("characteristic"))
                {
                    if (node.Attributes[0].Value == id.ToString())
                    {
                        for (int i = 1; i <= difference; i++)
                        {
                            XmlElement value_node = doc.CreateElement("value");
                            value_node.SetAttribute("Number", (max_degree + i).ToString());
                            XmlElement genes_node = doc.CreateElement("Genes");
                            XmlElement gene = doc.CreateElement("Gene");
                            GeneType g = null;
                            ComboBox cb = (ComboBox)tableLayoutPanel1.Controls[2 + 2 * i];
                            foreach (GeneType gg in gen_t.gene_table)
                            {
                                if (gg.name == (string)cb.SelectedItem)
                                {
                                    g = gg;
                                    break;
                                }
                            }
                            if (g == null)
                            {
                                MessageBox.Show("Value #" + i.ToString() + " has no link. Please, set it.");
                                return;
                            }
                            gene.SetAttribute("Id", g.allele_type_id.ToString());
                            gene.SetAttribute("Name", g.name);
                            TableLayoutPanel t = (TableLayoutPanel)tableLayoutPanel1.GetControlFromPosition(2, i);
                            gene.SetAttribute("Amount", "0");
                            decimal amount = 0;
                            for (int j = 0; j < t.RowCount; j++)
                            {
                                NumericUpDown num = (NumericUpDown)t.Controls[1 + 2 * j];
                                gene.SetAttribute("AlleleAmount" + j.ToString(), num.Value.ToString());
                                amount += num.Value;
                            }
                            gene.Attributes[2].Value = amount.ToString();
                            genes_node.AppendChild(gene);
                            value_node.AppendChild(genes_node);
                            node.ChildNodes[0].AppendChild(value_node);
                        }
                        break;
                    }
                }
            }
            doc.Save(link.path);
            this.Close();
        }
    }    
}
